package com.ynet.hmds28.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.taobao.pandora.boot.loader.util.StringUtils;
import com.ynet.hm.user.service.QueryProvideByUserIdService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.model.Serviceprovide;
import com.ynet.hmds28.user.model.ServiceprovideQuery;
import com.ynet.hmds28.user.model.extend.FieldClass;
import com.ynet.hmds28.user.model.extend.ServiceProvideExtend;
import com.ynet.hmds28.user.redis.RedisService;
@Transactional
@HSFProvider(serviceInterface = QueryProvideByUserIdService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class QueryProvideByUserIdServiceImpl implements
		QueryProvideByUserIdService {
	@Autowired
	private ServiceprovideDao serviceprovideDao;
	@Autowired
	private RedisService redisService;
	@Override
	public Result<?> queryProvideByUserId(String userId,String pageNo,String pageSize) {
		ServiceprovideQuery scq=new ServiceprovideQuery();
		scq.createCriteria().andUseridEqualTo(userId);
		int totalNum=serviceprovideDao.countByExample(scq);
		if(StringUtils.isEmpty(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isEmpty(pageSize)){
			pageSize="10";
		}
		scq.setPageNo(Integer.parseInt(pageNo));
		scq.setPageSize(Integer.parseInt(pageSize));
		scq.setStartRow((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
		List<Serviceprovide> sps=serviceprovideDao.selectByExample(scq);
		List<ServiceProvideExtend> spes=new ArrayList<>();
		if(!CollectionUtils.isEmpty(sps)){
			for(Serviceprovide sp:sps){
				ServiceProvideExtend spe=new ServiceProvideExtend();
				spe.setSeviceproviceid(sp.getSeviceproviceid());
				//从redis中取流量控制数
				String flowControl=(String) redisService.get(sp.getSeviceproviceid());
				spe.setFlowControl(flowControl);
				//查询输入字段
				List<FieldClass> importString=new ArrayList<>();
				String importField=sp.getImportfield();
				if(!StringUtils.isEmpty(importField)){
					String[] strs=importField.split(",");
					for(int i=0;i<strs.length;i++){
						FieldClass fc=new FieldClass();
						String[] str=strs[i].split("-");
						fc.setEnglishName(str[0]);
						fc.setChineseName(str[1]);
						importString.add(fc);
					}
				}
				spe.setImportField(importString);
				//查询输出字段
				List<FieldClass> exportString=new ArrayList<>();
				String exportField=sp.getExportfield();
				if(!StringUtils.isEmpty(exportField)){
					String[] strs=exportField.split(",");
					for(int i=0;i<strs.length;i++){
						FieldClass fc=new FieldClass();
						String[] str=strs[i].split("-");
						fc.setEnglishName(str[0]);
						fc.setChineseName(str[1]);
						exportString.add(fc);
					}
				}
				spe.setExportField(exportString);
				spe.setServicename(sp.getServicename());
				spe.setServicedesc(sp.getServicedesc());
				spe.setServicecostrule(sp.getServicecostrule());
				spes.add(spe);
			}
		}
		Map<String,Object> m=new HashMap<>();
		m.put("totalNum", totalNum);
		m.put("serviceProvide", spes);
		return ResultUtil.success(m);
	}

}
